<?php

namespace App\Http\Controllers\Api\v1\User;

use App\Models\Answer;
use App\Models\Api\User;
use App\Models\UserAnswer;
use Illuminate\Http\Request;

use App\Http\Controllers\Api\BaseController;
use Db,Cache;
class CertifyController extends BaseController
{

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取相关类型的认证题
     */
    public function index(){

        $user = User::where(['id'=>$request->uid])->first(['id','nickname','avatar','tel']);
        $data['xpCount'] = UserAnswer::where('type', 1)->count();
        $data['szCount'] = UserAnswer::where('type', 2)->count();
        $data['tlCount'] = UserAnswer::where('type', 3)->count();
        $data =[
            'user'=>$user,
            'xpCount'=> $data['xpCount'],
            'szCount'=> $data['szCount'],
            'tlCount'=> $data['tlCount']
        ];
         return show(200,"答题列表",$data);
    }
    public function confirmation(Request $request)
    {
        if(Cache::has('pass:' . $request->uid .':' .$request->get('type')))
           return show(403,"24小时内仅有一次认证的机会，请稍后再试！");
        $questions = UserAnswer::where('type', $request->get('type'))->get(['id', 'type', 'question', 'answer_one', 'answer_two', 'answer_three', 'answer_four']);
        return show(200,"答题",$questions);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 提交答题答案
     */
    public function user_confirm(Request $request)
    {

        //$request->get('answers');//等于$d
       // $d = array(['id' => 1, 'answer' => '全是'], ['id' => 4, 'answer' => '123']);
        $d = json_decode($request->get('answers'),true);
        $i = 0;
        $questions = UserAnswer::where('type', $request->get('type'))->get(['id', 'right_answer'])->toArray();
        foreach ($questions as $k => $v) {
            foreach ($d as $b => $c) {
                if (($questions[$k]['id'] == $d[$b]['id']) && $questions[$k]['right_answer'] == $d[$b]['answer'])
                $i++;
            }
        }
        $count = UserAnswer::where('type',$request->get('type'))->count();
        if ($i == $count){
            switch ($request->get('type')){
                case 1:
                    User::where('id',$request->uid)->update(['chart_state'=>1,'answers'=>$user->answers+10]);
                    return $this->success_msg('星盘认证成功');
                case 2:
                    User::where('id',$request->uid)->update(['dice_state'=>1,'answers'=>$user->answers+10]);
                    return $this->success_msg('骰子认证成功');
                case 3:
                    User::where('id',$request->uid)->update(['tarot_state'=>1,'answers'=>$user->answers+10]);
                    return $this->success_msg('塔罗认证成功');
            }
        }
        if ($i != $count){
             Cache::put('pass:' . $request->uid .':' .$request->get('type'),'-1',1440);
            $message = '认证失败，本次答对题目数：'.$i;
            return $this->errorr($message);
        }
    }

}
